//
//  LLLoginViewController.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLLoginViewController.h"
#import "LLLoginViewModel.h"

@interface LLLoginViewController () <UITextFieldDelegate>
@property(nonatomic, weak) IBOutlet UITextField *usernameTextField;
@property(nonatomic, weak) IBOutlet UITextField *passwordTextField;

@property(nonatomic, weak) IBOutlet UIButton *loginButton;

@property(nonatomic, strong, readonly) LLLoginViewModel *viewModel;
@end

@implementation LLLoginViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([SSKeychain rawLogin] != nil) {
        self.usernameTextField.text = [SSKeychain rawLogin];
        self.passwordTextField.text = [SSKeychain password];
    }

    // RAC之delegate实现
    @weakify(self)
    [[self
            rac_signalForSelector:@selector(textFieldShouldReturn:)
                     fromProtocol:@protocol(UITextFieldDelegate)]
            subscribeNext:^(RACTuple *tuple) {
                @strongify(self)
                if (tuple.first == self.passwordTextField) [self.viewModel.loginCommand execute:nil];
            }];

    self.passwordTextField.delegate = self;
}

- (void)bindViewModel {
    [super bindViewModel];

    @weakify(self)
    RAC(self.viewModel, username) = self.usernameTextField.rac_textSignal;
    RAC(self.viewModel, password) = self.passwordTextField.rac_textSignal;

    [self.viewModel.loginCommand.executing subscribeNext:^(NSNumber *executing) {
        @strongify(self)
        if (executing.boolValue) {
            [self.view endEditing:YES];
            [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES].labelText = @"Logging in...";
        } else {
            [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        }
    }];

    [self.viewModel.loginCommand.errors subscribeNext:^(NSError *error) {
        @strongify(self)
        NSString *message = @"Please enter the 2FA code you received via SMS or read from an authenticator app";
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Tips"
                                                                                 message:message
                                                                          preferredStyle:UIAlertControllerStyleAlert];

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.returnKeyType = UIReturnKeyGo;
            textField.placeholder = @"2FA code";
            textField.secureTextEntry = YES;
        }];

        [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:NULL]];

        [alertController addAction:[UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            @strongify(self)
            [self.viewModel.loginCommand execute:[alertController.textFields.firstObject text]];
        }]];

        [self presentViewController:alertController animated:YES completion:NULL];
    }];

//    RAC(self.loginButton, enabled) = self.viewModel.validLoginSignal;
    self.loginButton.rac_command = self.viewModel.loginCommand;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end
